package com.nuanshui.heatedloan.task.util;

import java.util.Set;

import com.nuanshui.heatedloan.task.constant.TaskGlobalParams;
import com.nuanshui.heatedloan.task.facade.bean.JobParams;

public class CommonUtil {
	
	/**
	 * 根据 定时任务的组合名称 获取 消息队列
	 * @param @param jobGroup
	 * @param @param jobName
	 * @param @return
	 * @author guohao
	 * @date 2017年1月4日 上午11:24:38
	 * @since $
	 */
	public static  String generateMessageQueueName(String jobGroup,String jobName) {
		
		StringBuffer sf =  new StringBuffer(TaskGlobalParams.JOB_PREFIX).append(jobGroup).append(".").append(jobName);
		return sf.toString();
		
	}
	
	public static String generateJobCode(JobParams jobParams) {
		 
		 int multiplier = 37;
		 long  hashcode = 17;
		 long checksum = 0;
		 int count = 0;
		 if(jobParams != null) {
			 Set<String> keys = jobParams.keySet();
			 for(String key :keys){
				 if(JobParams.JOB_CODE.equals(key)) continue;
				 Object object = jobParams.get(key);
				 int baseHashCode = object == null ? 1 : object.hashCode();
				    count++;
				    checksum += baseHashCode;
				    baseHashCode *= count;
				    hashcode = multiplier * hashcode + baseHashCode;
			 }
		 }		 
		 StringBuffer sb = new StringBuffer();
		 String   code =  sb.append(multiplier).append(hashcode).append(checksum).append(count).toString();
		 return code;
	}
	
	public static void main(String[] args) {
		JobParams jobParams = new JobParams();
		jobParams.setBizKey("aaa");
		jobParams.setJobCode("abc");
		jobParams.setJobGroup("zzzz");
		jobParams.setJobName("ooooo");
		System.out.println(generateJobCode(jobParams));
	}

}
